/**
 *
 * 解算服务API
 */
import type {DataForwardParseSelectParams, DataForwardParseServer} from "@/model/solutionService";
import type {Result, ResultList} from "@/service/port";
import service from "@/utils/https";


/**
 * 分页获取解算服务列表
 * @param params 分页参数
 * @returns 分类页列表
 */
export function getDataForwardParsesAPI(params: DataForwardParseSelectParams) {
    return service({
        method: "GET",
        url: "/dataForwardParse",
        params
    })
}

/**
 * 添加解算服务
 * @returns
 */
export function addDataForwardParseAPI(data: DataForwardParseServer) {
    return service({
        method: "POST",
        url: "/dataForwardParse",
        data
    })
}

/**
 * 修改解算服务
 */
export function updateDataForwardParseAPI(data: DataForwardParseServer) {
    return service({
        method: "PUT",
        url: `/dataForwardParse`,
        data
    })
}

/**
 * 删除解算服务
 */
export function deleteDataForwardParseAPI(id: number) {
    return service({
        method: "DELETE",
        url: `/dataForwardParse/${id}`
    })
}


/**
 * 物模型DTO
 */
export interface ThingModelDTO {
    deviceType: number,  // 设备类型
    deviceVersion: string, // 设备型号
    deviceProperties: string, // 设备属性
    remark: string // 备注
    collectTaskId: number // 任务采集器id
}

/**
 * 物模型
 */
export interface ThingModel extends ThingModelDTO {
    id: number
    createdAt: string // 创建时间
    updatedAt: string // 更新时间
}

/**
 * 添加物模型
 */
export function addThingModelAPI(data: ThingModelDTO) {
    return service({
        method: "POST",
        url: "/thingModel",
        data
    })
}

/**
 * 修改物模型
 */
export function updateThingModelAPI(data: ThingModel) {
    return service({
        method: "PUT",
        url: "/thingModel",
        data
    })
}

/**
 * id : 采集器id
 * id获取物模型
 */
export function getThingModelAPI(id: number) {
    return service({
        method: "GET",
        url: `/thingModel/${id}`
    })
}

/**
 * 解析器DTO
 */
export interface DataResolverDTO {
    status: boolean, // 状态
    remark: string // 备注
    name: string
    collectTaskId: number // 任务采集器id
    resolverCode: string // 解析器代码
}

/**
 * 解析器
 */
export interface DataResolver extends DataResolverDTO {
    id: number
    createdAt: string // 创建时间
    updatedAt: string // 更新时间
}

/**
 * 添加解析器
 */
export function addDataResolverAPI(data: DataResolverDTO) {
    return service({
        method: "POST",
        url: "/dataResolver",
        data
    })
}

/**
 *
 * 修改解析器
 */
export function updateDataResolverAPI(data: DataResolver) {
    return service({
        method: "PUT",
        url: "/dataResolver",
        data
    })
}

/**
 * id : 采集器id
 * 获取解析器
 */
export function getDataResolverAPI(id: number) {
    return service({
        method: "GET",
        url: `/dataResolver/${id}`
    })
}


/**
 * 处理器DTO
 */
export interface DataHandlerDTO {
    status?: boolean;
    remark?: string;
    collectTaskId?: number;
    dataHandlerCode?: string;
    dataHandlerNetWay?: number;
    dataHandlerType?: number;
    dataHandlerPublishType?: number;
    mqttUrl?: string;
    mqttTopic?: string;
    mqttUserName?: string;
    mqttPassword?: string;
    mqttQos?: number;
    httpUrlTokenUrl?: string;
    httpUrlTokenWay?: number;
    httpUrlTokenGetParams?: string;
    httpDataUrl?: string;
    httpDataUrlToken?: string;
    httpDataUrlOtherHeader?: string;
    httpDataWay?: number;
    tcpDataIp?: string;
    tcpDataPort?: number;
    otherInfo?: string;
}

/**
 * 处理器
 */
export interface HandlerListDTO {
    collectTaskId: number;
    pageNum: number;
    pageSize: number
}

/**
 * 处理器
 */
export interface DataHandler {
    id: number;
    createdAt: string;
    updatedAt: string;
    status: boolean;
    remark: string;
    collectTaskId: number;
    dataHandlerCode: string;
    dataHandlerNetWay: number;
    dataHandlerType: number;
    dataHandlerPublishType: number;
    mqttUrl: string;
    mqttTopic: string;
    mqttUserName: string;
    mqttPassword: string;
    mqttQos: number;
    httpUrlTokenUrl: string;
    httpUrlTokenWay: number;
    httpUrlTokenGetParams: string;
    httpDataUrl: string;
    httpDataUrlToken: string;
    httpDataUrlOtherHeader: string;
    httpDataWay: number;
    tcpDataIp: string;
    tcpDataPort: number;
    otherInfo: string;
}


/**
 * 获取指定ID的处理器数据。
 */
export function getDataHandlerAPI(params: HandlerListDTO): Promise<Result<ResultList<DataHandler[]>>> {
    return service({
        method: "GET",
        url: `/dataHandler`,
        params
    })
}

/**
 *
 * 添加处理器
 * @param data
 * @returns
 */
export function addDataHandlerAPI(data: DataHandlerDTO): Promise<Result<any>> {
    return service({
        method: "POST",
        url: "/dataHandler",
        data
    })
}

/**
 * 修改处理器
 * @param data
 * @returns
 */
export function updateDataHandlerAPI(data: DataHandler): Promise<Result<any>> {
    return service({
        method: "PUT",
        url: "/dataHandler",
        data
    })
}

/**
 * id删除
 * @param id 处理器id
 * @returns
 */
export function deleteDataHandlerAPI(id: number): Promise<Result<any>> {
    return service({
        method: "DELETE",
        url: `/dataHandler/${id}`
    })
}


/**
 * 调试解析器参数
 */
export interface HandlerDebugParams {
    handlerId: number;
    inputData: string
}

/**
 *
 * @param data 处理器测试
 * @returns
 */

export function dataHandlerDebugAPI(data: HandlerDebugParams): Promise<Result<any>> {
    return service({
        method: "POST",
        url: "/dataHandlerDebug",
        data
    })
}


/**
 * 采集日志参数。
 * @interface CollectLogParams
 * @property {number} collectTaskId - 采集任务ID
 * @property {number} pageNum - 页码
 * @property {number} pageSize - 每页条数
 * @property {string} startTime - 开始时间
 * @property {string} endTime - 结束时间
 */
export interface CollectLogParams {
    collectTaskId: number;
    handlerId?: number
    pageNum: number;
    pageSize: number;
    startTime: string;
    endTime: string;
}


/**
 * 采集日志实体。
 * @interface CollectLogEntity
 * @property {number} id - 日志ID
 * @property {string} createdAt - 创建时间
 * @property {number} collectTaskId - 采集任务ID
 * @property {boolean} executeStatus - 执行状态
 * @property {string} executeRes - 执行结果
 * @property {number} handlerDataId - 处理器ID
 * @property {string} saveData - 采集器执行失败保存的数据结果
 */
export interface CollectLogEntity {
    id: number;
    createdAt: string;
    collectTaskId: number;
    executeStatus: boolean;
    executeRes: string;
    handlerDataId: number;
    saveData: string;
}

/**
 * 采集日志实体列表。
 * @interface CollectLogEntityListVO
 * @property {CollectLogEntity[]} list - 日志实体列表
 * @property {number} total - 总记录数
 * @property {boolean} loading - 加载中
 */
export interface CollectLogEntityListVO {
    list: CollectLogEntity[];
    total: number;
    loading: boolean
}

/**
 * 采集日志API。
 *
 * @param {CollectLogParams} params - 采集日志参数
 * @return {Promise<Result<any>>} 采集日志结果
 */
export function collectLogAPI(params: CollectLogParams): Promise<Result<any>> {
    return service({
        method: "GET",
        url: `/collectLog`,
        params
    })
}

/**
 * 删除采集器日志
 */

export interface CollectLogDeleteParams {
    handlerId: number
    status: boolean
}

export function deleteCollectLogAPI(params: CollectLogDeleteParams): Promise<Result<any>> {
    return service({
        method: "DELETE",
        url: `/collectLog`,
        params
    })
}


/**
 *
 * @export  解析器测试参数
 * @interface DebugDecoderParams
 * @property {number} collectTaskId - 采集ID
 * @property {string} inputData - 测试数据
 *
 */
export interface DebugDecoderParams {
    collectTaskId: number;
    inputData: string;
}


/**
 * 采集任务调试API。
 *
 * @param {DebugDecoderParams} params - 采集任务调试参数
 * @return {Promise<Result<any>>} 采集任务调试结果
 */
export function collectTaskDebug(data: DebugDecoderParams) {
    return service({
        method: "POST",
        url: "/dataCollectTaskDebug",
        data
    })
}